/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sv.com.intesal.rrhh.managed;

import java.util.List;
import javax.faces.bean.ManagedBean;
import sv.com.intesal.clases.abstracts.ViewScopedAbstract;
import sv.com.intesal.clases.others.FormularioName.RRHH.Mtto;
import sv.com.intesal.rrhh.bean.Area;
import sv.com.intesal.rrhh.bean.Cargo;
import sv.com.intesal.rrhh.controlador.CargoControlador;

/**
 *
 * @author ejerez
 */
@ManagedBean(name = "cargoManaged")
public class CargoManaged extends ViewScopedAbstract {

    private Cargo selectedCargo;
    private List<Cargo> cargos;
    private List<Area> areas;

    public CargoManaged() {
        this.selectedCargo = new Cargo();
        this.areas = null;
        this.cargos = null;
        this.comboBox();
    }

    private void comboBox() {
        try {
            if (this.areas == null) {
                CargoControlador cc = new CargoControlador();
                this.areas = cc.getAreasActivos();
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    public void fillGrid() {
        try {
            if (this.cargos == null) {
                if (sm.validar_rol_usuario(Mtto.wfrmCargo, sm.buscar) == true) {
                    CargoControlador cc = new CargoControlador();
                    this.cargos = cc.getCargos();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    public void completeTransaction() {
        try {
            if (this.validar() == true) {
                if (this.selectedCargo.getCodigo_c() == null) {
                    if (sm.validar_rol_usuario(Mtto.wfrmCargo, sm.agregar) == true) {
                        CargoControlador cc = new CargoControlador();
                        cc.insert(this.selectedCargo);
                        m.messageInfo(m.getIinformation());
                        lm.limpiar();
                    }
                } else {
                    if (sm.validar_rol_usuario(Mtto.wfrmCargo, sm.modificar) == true) {
                        CargoControlador cc = new CargoControlador();
                        cc.update(this.selectedCargo);
                        m.messageInfo(m.getImodificada());
                        lm.limpiar();
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            m.messageError(m.getIerror());
        }
    }

    private Boolean validar() throws Exception {
        try {
            Boolean var_return = true;
            if (this.selectedCargo.getCodigo_a() == null) {
                var_return = false;
                m.messageAdvertencia(m.campoError("Area"));
            }
            if (this.selectedCargo.getNombre_c().equals("")) {
                var_return = false;
                m.messageAdvertencia(m.campoError("NOMBRE"));
            }
            return var_return;
        } catch (Exception e) {
            throw new Exception(e);
        }
    }

    /*SETTERS AND GETTERS*/
    public List<Area> getAreas() {
        return areas;
    }

    public void setAreas(List<Area> areas) {
        this.areas = areas;
    }

    public List<Cargo> getCargos() {
        return cargos;
    }

    public void setCargos(List<Cargo> cargos) {
        this.cargos = cargos;
    }

    public Cargo getSelectedCargo() {
        return selectedCargo;
    }

    public void setSelectedCargo(Cargo selectedCargo) {
        this.selectedCargo = selectedCargo;
    }
}
